import { userAccountLogin } from '@/api/user'
// 用户状态
export default {
  namespaced: true,
  state: () => ({
    profile: {
      id: '',
      avatar: '',
      nickname: '',
      account: '',
      mobile: '',
      token: ''
    }
  }),
  mutations: {
    // 测试持久化
    test (state, payload) {
      state.profile.id = payload
    },
    setUser (state, payload) {
      state.profile = payload
    },
    delUser (state) {
      state.profile = {}
    }
  },
  actions: {
    async asyncSetUser (ctx, useInfo) {
      const res = await userAccountLogin(useInfo)
      ctx.commit('setUser', res.result)
    },
    logout ({ commit }) {
      // 1. 清空用户数据  vuex
      commit('delUser')
      // 2. 清空购物车
      commit('cart/setList', [], { root: true })
    }
  }
}
